import json
import time

import requests

# 获取最新的歌曲
def new_song(self):
    # 获取内地歌曲
    song_url = 'http://musicapi.leanapp.cn/top/song?type=7'
    content = requests.get(song_url)
    ret = json.loads(content.text)
    new_song_list = []
    try:
        b = 0
        i = 0
        while i <= 28:
            song_time = ret['data'][b]['album']['publishTime']  # 发布时间
            song_pic = ret['data'][b]['album']['picUrl']  # 歌曲图片
            singer_name = ret['data'][b]['album']['artists'][0]['name']  # 歌手名字
            singer_id = ret['data'][b]['album']['artists'][0]['id']  # 歌手id
            song_name = ret['data'][b]['name']  # 歌曲名字
            song_id = ret['data'][b]['id']  # 歌曲id

            t = str(song_time)
            p = t[0:10]
            format = '%Y-%m-%d'
            value = time.localtime(int(p))
            hot = time.strftime(format, value)

            i += 1
            b += 1
            songs_dict = {
                'time': hot,
                'song_pic': song_pic,
                'singer_name': singer_name,
                'singer_id': singer_id,
                'song_name': song_name,
                'song_id': song_id,
            }

            new_song_list.append(songs_dict)
    except Exception as e:
        pass
    return new_song_list


# 获取飙升榜
def surge_song_top(self):
    surge_url = 'http://musicapi.leanapp.cn/top/list?idx=3'
    content = requests.get(surge_url)
    rets = json.loads(content.text)

    t = 0
    i = 0

    surge_top_list = []
    try:
        while i <= 2:
            surge_song_name = rets['playlist']['tracks'][t]['name']  # 歌曲名字
            surge_song_id = rets['playlist']['tracks'][t]['id']  # 歌曲id
            surge_singer_id = rets['playlist']['tracks'][t]['ar'][0]['id']  # 歌手id
            surge_singer_name = rets['playlist']['tracks'][t]['ar'][0]['name']  # 歌手名字

            t += 1
            i += 1
            date = {
                'surge_song_name': surge_song_name,
                'surge_song_id': surge_song_id,
                'surge_singer_id': surge_singer_id,
                'surge_singer_name': surge_singer_name,
                'num': t,
            }
            surge_top_list.append(date)
    except Exception as e:
        pass

    return surge_top_list


# 获取热歌榜排行
def hot_song_top(self):
    hot_song_url = 'http://musicapi.leanapp.cn/top/list?idx=1'
    content = requests.get(hot_song_url)
    rets = json.loads(content.text)

    t = 0
    i = 0

    hot_top_list = []
    try:
        while i <= 2:
            surge_song_name = rets['playlist']['tracks'][t]['name']  # 歌曲名字
            surge_song_id = rets['playlist']['tracks'][t]['id']  # 歌曲id
            surge_singer_id = rets['playlist']['tracks'][t]['ar'][0]['id']  # 歌手id
            surge_singer_name = rets['playlist']['tracks'][t]['ar'][0]['name']  # 歌手名字

            t += 1
            i += 1
            date = {
                'surge_song_name': surge_song_name,
                'surge_song_id': surge_song_id,
                'surge_singer_id': surge_singer_id,
                'surge_singer_name': surge_singer_name,
                'num': t,
            }
            hot_top_list.append(date)
    except Exception as e:
        pass

    return hot_top_list


# 获取新歌榜排行
def new_song_top(self):
    new_song_url = 'http://musicapi.leanapp.cn/top/list?idx=1'
    content = requests.get(new_song_url)
    rets = json.loads(content.text)

    t = 0
    i = 0

    new_top_list = []

    try:
        while i <= 2:
            surge_song_name = rets['playlist']['tracks'][t]['name']  # 歌曲名字
            surge_song_id = rets['playlist']['tracks'][t]['id']  # 歌曲id
            surge_singer_id = rets['playlist']['tracks'][t]['ar'][0]['id']  # 歌手id
            surge_singer_name = rets['playlist']['tracks'][t]['ar'][0]['name']  # 歌手名字

            t += 1
            i += 1
            date = {
                'surge_song_name': surge_song_name,
                'surge_song_id': surge_song_id,
                'surge_singer_id': surge_singer_id,
                'surge_singer_name': surge_singer_name,
                'num': t,
            }
            new_top_list.append(date)
            # ten_top_list.append(data)
    except Exception as e:
        pass
    return new_top_list


# 获取韩国排行榜
def korea_song_top(self):
    Korea_song_url = 'http://musicapi.leanapp.cn/top/list?idx=11'
    content = requests.get(Korea_song_url)
    rets = json.loads(content.text)

    t = 0
    i = 0

    Korea_top_list = []
    try:
        while i <= 2:
            surge_song_name = rets['playlist']['tracks'][t]['name']  # 歌曲名字
            surge_song_id = rets['playlist']['tracks'][t]['id']  # 歌曲id
            surge_singer_id = rets['playlist']['tracks'][t]['ar'][0]['id']  # 歌手id
            surge_singer_name = rets['playlist']['tracks'][t]['ar'][0]['name']  # 歌手名字

            t += 1
            i += 1
            date = {
                'surge_song_name': surge_song_name,
                'surge_song_id': surge_song_id,
                'surge_singer_id': surge_singer_id,
                'surge_singer_name': surge_singer_name,
                'num': t,
            }
            Korea_top_list.append(date)
    except Exception as e:
        pass

    return Korea_top_list


# 获取欧美排行榜
def EUROPE_song_top(self):
    EUROPE_song_url = 'http://musicapi.leanapp.cn/top/list?idx=6'
    content = requests.get(EUROPE_song_url)
    rets = json.loads(content.text)

    t = 0
    i = 0

    EUROPE_top_list = []
    try:
        while i <= 2:
            surge_song_name = rets['playlist']['tracks'][t]['name']  # 歌曲名字
            surge_song_id = rets['playlist']['tracks'][t]['id']  # 歌曲id
            surge_singer_id = rets['playlist']['tracks'][t]['ar'][0]['id']  # 歌手id
            surge_singer_name = rets['playlist']['tracks'][t]['ar'][0]['name']  # 歌手名字

            t += 1
            i += 1
            date = {
                'surge_song_name': surge_song_name,
                'surge_song_id': surge_song_id,
                'surge_singer_id': surge_singer_id,
                'surge_singer_name': surge_singer_name,
                'num': t,
            }
            EUROPE_top_list.append(date)
    except Exception as e:
        pass

    return EUROPE_top_list